﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for GPSLib
/// </summary>
public static class GPSLib
{
    public static String AddInstamaperPlots(DataTable tblPoints, string keyForChange, string Colorsformap, bool IsLineDraw, bool IsNameVisible, string DelivererName)
    {
        String Locations = "";
        String sJScript = "";
       
        string time = string.Empty;
        try
        {

            int CountOfPointStanding = 0;
            string Image = string.Empty;
            for (int i = 0; i < tblPoints.Rows.Count; i++)
            {
                System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                // bypass empty rows 

                if (tblPoints.Columns["latitude"].ToString().Trim().Length == 0)
                    continue;

                string Latitude = tblPoints.Rows[i]["latitude"].ToString();
                string Longitude = tblPoints.Rows[i]["longitude"].ToString();

                try
                {
                    if (tblPoints.Rows[i]["timestamp"].ToString() != "0.0")
                    {
                        dtDateTime = dtDateTime.AddSeconds(Convert.ToInt32(tblPoints.Rows[i]["timestamp"].ToString()));
                        time = dtDateTime.AddHours(-5).ToShortTimeString();
                       
                       
                    }
                    else
                        time = "";
                }
                catch (Exception ex)
                {

                    time = "";
                }
                if (i != 0)
                {
                    string Previouslatitude = tblPoints.Rows[i - 1]["latitude"].ToString();
                    string Previouslongitude = tblPoints.Rows[i - 1]["longitude"].ToString();
                    if (((Convert.ToDecimal(Latitude) - Convert.ToDecimal(Previouslatitude)) < Convert.ToDecimal(.00025)) && ((Convert.ToDecimal(Latitude) - Convert.ToDecimal(Previouslongitude)) < Convert.ToDecimal(.00025)))
                    {
                        CountOfPointStanding = CountOfPointStanding + 1;
                    }
                    else
                    {
                        CountOfPointStanding = 0;
                    }

                }
                if (CountOfPointStanding >= 5)
                {
                    Image = "images/skull.png";
                }
                else
                {
                    Image = "images/yellowmarker.png";
                }
                if (i == tblPoints.Rows.Count-1)
                {
                    Image = "images/redmarker.png";
                }
                // create a line of JavaScript for marker on map for this record 
                Locations += Environment.NewLine + @"
                        
                path.push(new google.maps.LatLng(" + Latitude + ", " + Longitude + @"));
                 marker" + keyForChange + i + @"= new google.maps.Marker({
                    position: new google.maps.LatLng(" + Latitude + ", " + Longitude + @"),
                    icon: '" + Image + @"',  
                    draggable: false,
                    strokeColor: '#FF0000',
                    title:'#" + i + "," + DelivererName + ",*" + time + "*," + tblPoints.Rows[i]["speed"].ToString() + @"km/h',
                    map: map    
                }); markersArray.push(marker" + keyForChange + i + ");";
                if (i == tblPoints.Rows.Count-1)
                {
                    Locations += "marker" + keyForChange + i + ".setZIndex(marker" + keyForChange + i + ".getZIndex() + 500);";
                }
                Locations += @"google.maps.event.addListener( marker" + keyForChange + i + @", 'dblclick', function(event) {
                $('table.hide').hide();  $('#table" + keyForChange + @"').show();                
                });";


            }

            sJScript = @"var poly" + keyForChange + "; var polyOptions" + keyForChange + " = {strokeColor: '" + Colorsformap + "',strokeOpacity: 0.5, strokeWeight: 3}; poly" + keyForChange + " = new google.maps.Polyline(polyOptions" + keyForChange + ");poly" + keyForChange + ".setMap(map);var path = poly" + keyForChange + ".getPath();" + Locations + @" ";

        }
        catch (Exception ex)
        {
        }
        return sJScript;
    }
    public static String AddInstamaperPlotsTable(DataTable tblPoints, Literal litTablHTML, string keyForChange)
    {
        String sHTMLTable = "";
        String sJqueryTable = "";
        string time = string.Empty;

        for (int i = 0; i < tblPoints.Rows.Count; i++)
        {
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            try
            {
                if (tblPoints.Rows[i]["timestamp"].ToString() != "0.0")
                {

                    dtDateTime = dtDateTime.AddSeconds(Convert.ToInt32(tblPoints.Rows[i]["timestamp"].ToString()));
                    time = dtDateTime.ToShortDateString() + " " + dtDateTime.AddHours(-5).ToShortTimeString();

                }
                else
                    time = "";
            }
            catch (Exception ex)
            {

                time = "";
            }
            sHTMLTable += "<tr class=\"hoverclass\" id='trmarker" + keyForChange + i + "' markerid= 'marker" + keyForChange + i + "'><td>" + time + @"</td>
                           <td>" + tblPoints.Rows[i]["speed"].ToString() + @"km/h</td>";

            sJqueryTable += "$('#trmarker" + keyForChange + i + "').live(\"mouseover\", function () {removeAllBackgroung(this);removeAllRedMarker(this); marker" + keyForChange + i + ".setIcon('images/redmarker1.png');marker" + keyForChange + i + ".setZIndex(marker" + keyForChange + i + ".getZIndex() + 1000);if(!map.getBounds().contains(marker" + keyForChange + i + ".getPosition())){map.setCenter(marker" + keyForChange + i + ".getPosition());} });";
            //sJqueryTable += "$('#trmarker" + keyForChange + i + "').live(\"mouseout\", function () {marker" + keyForChange + i + ".setIcon('images/yellowmarker.png');marker" + keyForChange + i + ".setZIndex(marker" + keyForChange + i + ".getZIndex() - 1000) });";

        }


        litTablHTML.Text += "<table class=\"hide\"  width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" id=\"table" + keyForChange + "\" style=\"display:none;border:1px solid gray;font-size\"11px\"padding:2px 10px 2px 10px;text-align:center\"><tr><th>Time</th><th>Speed</th></tr>  " + sHTMLTable + "</table><script>" + sJqueryTable + "</script>";

        return "";
    }
    public static String AddCutomerMarkerAndInfo(DataTable tblPoints, string keyForChange, string Colorsformap, bool IsLineDraw, bool IsNameVisible)
    {
        string Title = string.Empty;
        StringBuilder cstext2Merchant = new StringBuilder();

        foreach (DataRow r in tblPoints.Rows)
        {
            // bypass empty rows 

            if (r["latitude"].ToString().Trim().Length == 0 || r["longitude"].ToString().Trim().Length == 0)
                continue;
            string Latitude = r["latitude"].ToString();
            string Longitude = r["longitude"].ToString();

            //Title = "<table border=0px ><tr><td style=\"vertical-align: top;></td>" + Longitude + "</tr></table>";
            cstext2Merchant.Append("marker = new google.maps.Marker({position: new google.maps.LatLng(" + Latitude + "," + Longitude + "),map: map");
            if (r["OK"].ToString() != "OK")
                cstext2Merchant.Append(",icon: 'images/" + Colorsformap + ".png',  draggable: false,");
            else
                cstext2Merchant.Append(",icon: 'images/green.png',  draggable: false,");
            cstext2Merchant.Append(" title:'" + r["Nombre_Moral_Fisica"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + r["Accion_a_Realizar"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "  " + r["Comentarios"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "'");
            if (IsNameVisible)
            {
                cstext2Merchant.Append("});google.maps.event.addListener(marker, 'click', (function (marker, i) {return function () {infowindow.setContent(");
                cstext2Merchant.Append("'<div><p>Nombre_Moral_Fisica: " + r["Nombre_Moral_Fisica"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Numero_de_Cliente: " + r["Numero_de_Cliente"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Numero_SAE: " + r["Numero_SAE"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Ejecutivo: " + r["Ejecutivo"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Accion_a_Realizar: " + r["Accion_a_Realizar"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Comentarios: " + r["Comentarios"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Colonia: " + r["Colonia"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br>N: " + Latitude + "<br/>W: " + Longitude + "</p></div>'");
                cstext2Merchant.Append(");infowindow.open(map, marker);}})(marker, i)); ");
            }
            else
                cstext2Merchant.Append(" });");

            cstext2Merchant.Append("var myLatLng = new google.maps.LatLng(" + Latitude + "," + Longitude + ");");
            cstext2Merchant.Append("map.setCenter(myLatLng);");
            cstext2Merchant.Append(Environment.NewLine);

        }

        return cstext2Merchant.ToString();
    }

    public static String AddCutomerMarkerAndInfoforcobrar(DataTable tblPoints, string keyForChange, string Colorsformap, bool IsLineDraw, bool IsNameVisible)
    {
        string Title = string.Empty;
        StringBuilder cstext2Merchant = new StringBuilder();

        foreach (DataRow r in tblPoints.Rows)
        {

            // bypass empty rows 
            if (r["Accion_a_Realizar"].ToString().Trim().ToLower().Contains("cobrar"))
            {
                if (r["latitude"].ToString().Trim().Length == 0 || r["longitude"].ToString().Trim().Length == 0)
                    continue;
                string Latitude = r["latitude"].ToString();
                string Longitude = r["longitude"].ToString();

                //Title = "<table border=0px ><tr><td style=\"vertical-align: top;></td>" + Longitude + "</tr></table>";
                cstext2Merchant.Append("marker = new google.maps.Marker({position: new google.maps.LatLng(" + Latitude + "," + Longitude + "),map: map");
                if (r["OK"].ToString() != "OK")
                    cstext2Merchant.Append(",icon: 'images/" + Colorsformap + ".png',  draggable: false,");
                else
                    cstext2Merchant.Append(",icon: 'images/green.png',  draggable: false,");
                cstext2Merchant.Append(" title:'" + r["Nombre_Moral_Fisica"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + r["Accion_a_Realizar"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "  " + r["Comentarios"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "'");
                if (IsNameVisible)
                {
                    cstext2Merchant.Append("});google.maps.event.addListener(marker, 'click', (function (marker, i) {return function () {infowindow.setContent(");
                    cstext2Merchant.Append("'<div><p>Nombre_Moral_Fisica: " + r["Nombre_Moral_Fisica"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Numero_de_Cliente: " + r["Numero_de_Cliente"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Numero_SAE: " + r["Numero_SAE"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Ejecutivo: " + r["Ejecutivo"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Accion_a_Realizar: " + r["Accion_a_Realizar"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Comentarios: " + r["Comentarios"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Colonia: " + r["Colonia"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br>N: " + Latitude + "<br/>W: " + Longitude + "</p></div>'");
                    cstext2Merchant.Append(");infowindow.open(map, marker);}})(marker, i)); ");
                }
                else
                    cstext2Merchant.Append(" });");

                cstext2Merchant.Append("var myLatLng = new google.maps.LatLng(" + Latitude + "," + Longitude + ");");
                cstext2Merchant.Append("map.setCenter(myLatLng);");
                cstext2Merchant.Append(Environment.NewLine);
            }
        }

        return cstext2Merchant.ToString();
    }
    public static String AddCutomerMarkerAndInfoByPagos(DataTable tblPoints, string keyForChange, string Colorsformap, bool IsLineDraw, bool IsNameVisible)
    {
        string Title = string.Empty;
        StringBuilder cstext2Merchant = new StringBuilder();

        foreach (DataRow r in tblPoints.Rows)
        {
            // bypass empty rows 

            if (r["Latitude"].ToString().Trim().Length == 0 || r["Longitude"].ToString().Trim().Length == 0)
                continue;
            string Latitude = r["Latitude"].ToString();
            string Longitude = r["Longitude"].ToString();

            //Title = "<table border=0px ><tr><td style=\"vertical-align: top;></td>" + Longitude + "</tr></table>";
            cstext2Merchant.Append("marker = new google.maps.Marker({position: new google.maps.LatLng(" + Latitude + "," + Longitude + "),map: map");
            cstext2Merchant.Append(",icon: 'images/icon50.png',  draggable: false,");
            cstext2Merchant.Append(" title:'Efectivo $: " + r["Importe_en_Efectivo"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + " Cheques $: " + r["Importe_en_Cheque"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "'");
            if (IsNameVisible)
            {
                cstext2Merchant.Append("});google.maps.event.addListener(marker, 'click', (function (marker, i) {return function () {infowindow.setContent(");
                cstext2Merchant.Append("'<div><p>Nombre_Moral_Fisica: " + r["Nombre_Moral_Fisica"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Numero_de_Cliente: " + r["Numero_de_Cliente"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Numero_SAE: " + r["Numero_SAE"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/>Ejecutivo: " + r["Ejecutivo"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Added_Time: " + r["Added_Time"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Added_User: " + r["Added_User"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br/><br/>Importe_en_Cheque: " + r["Importe_en_Cheque"].ToString().Replace("\'", "&#39;").Replace('\n', ' ') + "<br>N: " + Latitude + "<br/>W: " + Longitude + "</p></div>'");
                cstext2Merchant.Append(");infowindow.open(map, marker);}})(marker, i)); ");
            }
            else
                cstext2Merchant.Append(" });");

            cstext2Merchant.Append("var myLatLng = new google.maps.LatLng(" + Latitude + "," + Longitude + ");");
            cstext2Merchant.Append("map.setCenter(myLatLng);");
            cstext2Merchant.Append(Environment.NewLine);

        }

        return cstext2Merchant.ToString();
    }

}
